'use strict';

let path = require("path"),
    fs = require("fs"),
    http = require("http"),
    url = require("url");

let root = path.resolve(process.argv[2] || '.');   //process?  这个是什么???

let server = http.createServer((request,response)=>{
    let pathname = url.parse(request.url).pathname;
    let filePath = path.join(root,pathname);
    fs.stat(filePath,function(error,stats){
        console.log(url.parse(request.url));
        if( !error && stats.isFile() ){
            response.writeHead(200);
            fs.createReadStream(filePath).pipe(response);
        }else{
            console.log('404'+request.url);
            response.writeHead(404);
            response.end("404 Not Found")
        }
    })
});

server.listen(6685);
console.log('Server is running at http://127.0.0.1:6685/');